﻿using System;
using System.Xml;
using System.Xml.Serialization;

namespace GISUtil.KML
{
  /// <summary>
  /// Contains methods for serializing and deserializing information founnd in the Icon section of KML
  /// </summary>
  public class KMLIcon
  {
    /// <summary>
    /// the unique ID of this style.
    /// </summary>
    [XmlAttribute("Id")]
    public string Id { get; set; }

    /// <summary>
    /// The URL of the icon to use. Can be absolute or relative (to router's configured icon location)
    /// </summary>
    /// <remarks>
    /// If an absolute path is provided, it will be stored as-is. If a relative path is provided, it
    /// will be assumed to be relative the the KML icon storage location specified in the router's
    /// configuration, and that configured path will be prepended to the given value.
    /// </remarks>
    [XmlElement("href")]
    public string href
    {
      get
      {
        return _href;
      }
      set
      {
        if (string.IsNullOrWhiteSpace(value))
        {
          _href = null;
        }
        else
        {
          Uri absolute;
          if (Uri.TryCreate(value, UriKind.Absolute, out absolute))
          {
            _href = value;
          }
          else
          {
            string uri = ICNETServices.AWSConstants.KMLIconURL;
            if (uri.EndsWith("/") && value.StartsWith("/")) //They both have a slash
            {
              _href = uri + value.Substring(1);
            }
            else if (uri.EndsWith("/") || value.StartsWith("/")) //Only one has a slash
            {
              _href = uri + value;
            }
            else //niether has a slash
            {
              _href = uri + "/" + value;
            }
          }
        }
      }
    }
    private string _href;
    /* 
     * KML reference https://developers.google.com/kml/documentation/kmlreference#iconstyle indicates that href should be the only 
     * property of an Icon in an IconStyle. None of the styles we have use any of these fields anyway
     * 
    /// <summary>
    /// If the href specifies an Icon palette, X  identifies the X offset, in Pixels, from the lower-left corner of the Icon palette.
    /// </summary>
    [XmlElement("gx : X")]
    public int? OffsetX { get; set; }

    /// <summary>
    /// If the href specifies an Icon palette, Y identifies the Y offset, in Pixels, from the lower-left corner of the Icon palette.
    /// </summary>
    [XmlElement("gx : Y")]
    public int? OffsetY { get; set; }

    /// <summary>
    /// If the href specifies an Icon palette, this variable specifies the Width.
    /// </summary>
    [XmlElement("gx : w")]
    public int? Width { get; set; }

    /// <summary>
    /// If the href specifies an Icon palette, this variable specifies the Height.
    /// </summary>
    [XmlElement("gx : h")]
    public int? Height { get; set; }
    
    /// <summary>
    /// The RefreshMode associated with this styl.e
    /// </summary>
    [XmlElement("refreshMode")]
    public RefreshMode? RefreshMode { get; set; }

    /// <summary>
    /// The RefreshMode associated with this style.
    /// </summary>
    [XmlElement("refreshInterval")]
    public float? RefreshInterval { get; set; }

    /// <summary>
    /// The ViewRefreshMode associated with this style.
    /// </summary>
    [XmlElement("viewRefreshMode")]
    public ViewRefreshMode? ViewRefreshMode { get; set; }

    /// <summary>
    /// The ViewRefreshTime associated with this style.
    /// </summary>
    [XmlElement("viewRefreshTime")]
    public float? ViewRefreshTime { get; set; }

    /// <summary>
    /// The amount by which hte bounding box should be scaled.
    /// </summary>
    [XmlElement("viewBoundScale")]
    public float? ViewBoundScale { get; set; }

    /// <summary>
    /// Contains optional viewing paramters as well as optoinal version and language information.
    /// </summary>
    [XmlElement("viewFormat")]
    public string ViewFormat { get; set; }

    /// <summary>
    /// Contiains the http query to be sent to the server. 
    /// </summary>
    [XmlElement("httpQuery")]
    public string HttpQuery { get; set; }
    */

    /// <summary>
    /// Determines if this class has any styling information associated with it. 
    /// </summary>
    /// <returns>True if at least one of the fields that contains style information has a value, otherwise false.</returns>
    public bool HasAValue()
    {
      return Id != null ||
        href != null/* ||
        OffsetX.HasValue ||
        OffsetY.HasValue ||
        Width.HasValue ||
        Height.HasValue ||
        RefreshMode.HasValue ||
        RefreshInterval.HasValue ||
        ViewRefreshMode.HasValue ||
        ViewRefreshTime.HasValue ||
        ViewBoundScale.HasValue ||
        ViewFormat != null ||
        HttpQuery != null*/;
    }
  }
}